﻿using System;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI;
//using System.Web.UI.WebControls;
using QTMusic.BaseClasses;

namespace QTMusic.UserControls
{
    public partial class RequestListItem : System.Web.UI.UserControl
    {
        private int fromId = 0;
        private int userId = 0;

        public int FromId
        {
            get { return fromId; }
            set { fromId = value; }
        }

        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        public void cmdDeny_Click(object sender, EventArgs e)
        {
            string updateSql = "UPDATE [friendreq] SET "
                                + "friendreq_status = @status "
                                + "WHERE friendreq_from_id = @fromId "
                                + "AND user_id = @userId";
            SqlConnection conn = new SqlConnection(Common.connString);
            SqlCommand cmd = new SqlCommand(updateSql, conn);
            cmd.Parameters.AddWithValue("@status", "Denied");
            cmd.Parameters.AddWithValue("@fromId", fromId);
            cmd.Parameters.AddWithValue("@userId", userId);

            int updated = 0;
            try
            {
                using (conn)
                {
                    conn.Open();
                    updated = cmd.ExecuteNonQuery();
                }
            }
            catch (Exception requestListEx)
            {
                //RequestListItemException newException =
                //            new RequestListItemException(requestListEx.Message, requestListEx);
                //throw newException;
                Common.WriteToEventLog(requestListEx, "LoadRequestListItem");
            }

            if (updated > 0)
            {
                pnlRequestItem.Controls.Clear();
                LiteralControl objReport = new LiteralControl("Yêu cầu đã bị từ chối.");
                pnlRequestItem.Controls.Add(objReport);
            }
        }

        public void cmdAccept_Click(object sender, EventArgs e)
        {
            string updateSql = "UPDATE [friendreq] SET "
                                + "friendreq_status = @status "
                                + "WHERE friendreq_from_id = @fromId "
                                + "AND user_id = @userId";
            SqlConnection conn = new SqlConnection(Common.connString);
            SqlCommand cmd = new SqlCommand(updateSql, conn);
            cmd.Parameters.AddWithValue("@status", "Accepted");
            cmd.Parameters.AddWithValue("@fromId", fromId);
            cmd.Parameters.AddWithValue("@userId", userId);

            int updated = 0;
            /*
            try
            {
                using (conn)
                {
                    conn.Open();
                    updated = cmd.ExecuteNonQuery();
                }
            }
            catch (Exception requestListEx)
            {
                //RequestListItemException newException =
                //            new RequestListItemException(requestListEx.Message, requestListEx);
                //throw newException;
                Common.WriteToEventLog(requestListEx, "LoadRequestListItem");
            }*/
            conn.Open();
            updated = cmd.ExecuteNonQuery();
            conn.Close();

            if (updated > 0)
            {
                string insertSql = "INSERT INTO [friend] VALUES (@userId, @friendId)";
                cmd = new SqlCommand(insertSql, conn);
                cmd.Parameters.AddWithValue("@userId", userId);
                cmd.Parameters.AddWithValue("@friendId", fromId);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                cmd = new SqlCommand(insertSql, conn);
                cmd.Parameters.AddWithValue("@userId", fromId);
                cmd.Parameters.AddWithValue("@friendId", userId);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                pnlRequestItem.Controls.Clear();
                LiteralControl objReport = new LiteralControl("Yêu cầu đã được chấp nhận.");
                pnlRequestItem.Controls.Add(objReport);
                // TODO: chọn nhóm
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string requesterUrl = Common.urlWebsites + "Profile.aspx?id=" + fromId.ToString();

            lnkRequestFrom.NavigateUrl = requesterUrl;
            lnkRequestFrom.Text = Common.GetUserName(fromId);
            if (lnkRequestFrom.Text == "")
            {
                lnkRequestFrom.Text = "Unknown";
                lnkRequestFrom.Enabled = false;
            }
        }
    }

    public class RequestListItemException : ApplicationException
    {
        public RequestListItemException()
            : base()
        {

        }

        public RequestListItemException(string message)
            : base(message)
        {

        }

        public RequestListItemException(string message, Exception inner)
            : base(message, inner)
        {

        }
    }
}